from typing import Any

from db_pool import SF_DB

batch_size = 1000


def put_yc_result_data(out_data: Any):
    if not out_data:
        return
    db = SF_DB()
    with db.get_pool().get_connection() as conn:
        conn.begin()
        cursor = conn.cursor()
        # 定义要插入的数据
        data_list = [tuple(d.values()) for d in out_data]
        add_time_list = [out_data[0]['addTime']]
        # 先删除之前生成数据
        cursor.execute('DELETE FROM wops_sku_forecast WHERE add_date = %s', add_time_list)
        # 执行批量插入操作
        for i in range(0, len(data_list), batch_size):
            data_batch = data_list[i:i + batch_size]
            cursor.executemany("INSERT INTO wops_sku_forecast "
                               "(id , work_time , supplier , sku , "
                               "pack_type, in_out_type, warehouse_type, quantity , add_date) "
                               "VALUES (%s , %s, %s, %s, %s, %s, %s, %s, %s)", data_batch)
        # 提交事务并关闭连接
        conn.commit()
